<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="cancelBtn">
    <el-form
      :model="form"
      ref="addForm"
      label-width="80px"
      style="width: 75%; margin: 0 auto"
      :rules="rules"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="form.username"
          autocomplete="off"
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="form.mobile"
          autocomplete="off"
          placeholder="请输入手机号"
        ></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="date">
        <el-date-picker
          style="width: 100%"
          v-model="form.timeOfEntry"
          type="date"
          placeholder="选择入职时间"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          style="width: 100%"
          v-model="form.formOfEmployment"
          placeholder="请选择聘用形式"
        >
          <el-option
            v-for="item in hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="form.workNumber"
          autocomplete="off"
          placeholder="请输入工号"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="form.departmentName"
          autocomplete="off"
          @focus="showTreeData"
          @blur="handleBlur"
          placeholder="请选择部门"
        ></el-input>
        <!-- 放一个tree来显示部门 -->
        <el-tree
          v-if="treeShow"
          v-loading="treeLoading"
          default-expand-all
          :data="departments"
          :props="defaultProps"
          @node-click="nodeClick"
        ></el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          style="width: 100%"
          v-model="form.correctionTime"
          type="date"
          placeholder="选择转正时间"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelBtn">取 消</el-button>
      <el-button type="primary" @click="addDept">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDepartments } from "@/api/departments";
import constant from "@/api/constant/employees";
import { tranListToTreeData } from "@/utils/index";
import { addEmp } from "@/api/employees";
export default {
  name: "add-emp",
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      treeShow: false,
      treeLoading: false,
      departments: [],
      defaultProps: {
        label: "name",
        children: "children",
      },
      form: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        correctionTime: "",
        departmentName: "",
      },
      hireType: constant.hireType,
      rules: {
        username: [
          { required: true, message: "请输入员工姓名", trigger: "blur" },
          { min: 1, max: 5, message: "只能输入1-5个字符", trigger: "blur" },
        ],

        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            trigger: "blur",
            message: "请输入正确的手机号格式",
          },
        ],
        timeOfEntry: [
          { required: true, message: "请选择入职时间", trigger: "blur" },
        ],
        formOfEmployment: [
          { required: true, message: "请选择聘用形式", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "请输入员工工号", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "请选择部门", trigger: "change" },
        ],
        correctionTime: [
          { required: true, message: "请选择转正时间", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    cancelBtn() {
      this.form = {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        correctionTime: "",
        departmentName: "",
      };

      // 重置表单
      this.$refs.addForm.resetFields();
      // 隐藏对话框
      this.$emit("update:showDialog", false);
    },
    async showTreeData() {
      this.treeShow = true;
      this.treeLoading = true;
      let res = await getDepartments();
      this.departments = tranListToTreeData(res.data.depts, "");
      this.treeLoading = false;
    },
    nodeClick(data, node, tree) {
      this.form.departmentName = data.name;
      this.treeShow = false;
    },
    handleBlur(e) {
      if (
        !e.relatedTarget ||
        e.relatedTarget.className.indexOf("el-tree-node") == -1
      ) {
        this.treeShow = false;
      }
    },
    addDept() {
      this.$refs.addForm.validate(async (v) => {
        if (v) {
          await addEmp(this.form);

          this.$message.success("增加成功");

          let pageObj = this.$parent.pageInfo;

          pageObj.total++;
          this.$parent.pageInfo.page = Math.ceil(pageObj.total / pageObj.size);
          console.log(this.$parent);

          this.$parent.getEmployeeList();
          this.$emit("update:showDialog", false);
        }
      });
    },
  },
};
</script>

<style>
</style>